/**
 *   Copyright 2008 Alexey Vishentsev
 *
 *   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

package com.gwtspreadsheetinput.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

public class DynGridData {
	private JavaScriptObject data;
	
	private JsArrayString colNames;
	private JsArrayString colIds;
	private ControlWrapper[] colWidthControls;
	private int colCount;
	private ControlWrapper scrollXControl;
	private ControlWrapper scrollYControl;
	private ControlWrapper selectionRowControl;
	private ControlWrapper selectionColControl;
	private int spreadSheetWidth;
	private int spreadSheetHeight;
	
	
	public DynGridData(String varPrefix) {
		initDataObject(varPrefix+"_params");
		colNames = getStringArrayParam("colNames");
		colIds = getStringArrayParam("colIds");
		colCount = colNames.length();
		colWidthControls = new ControlWrapper[colCount];
		JsArrayString colWidthControlIds = getStringArrayParam("colWidthContorlIds");
		for(int i = 0; i < colCount; i++) {
			colWidthControls[i] = createControlWrapper(colWidthControlIds.get(i));
		}
		String scrollXId = getStringParam("scrollXId");
		Element scrollXElem = DOM.getElementById(scrollXId);
		scrollXControl = new ControlWrapper(scrollXElem);
		
		String scrollYId = getStringParam("scrollYId");
		Element scrollYElem = DOM.getElementById(scrollYId);
		scrollYControl = new ControlWrapper(scrollYElem);
		
		String selectionRowId = getStringParam("selectionRow");
		Element selectionRowElem = DOM.getElementById(selectionRowId);
		selectionRowControl = new ControlWrapper(selectionRowElem);
		
		String selectionColId = getStringParam("selectionCol");
		Element selectionColElem = DOM.getElementById(selectionColId);
		selectionColControl = new ControlWrapper(selectionColElem);
		
		spreadSheetWidth = getIntParam("width");
		spreadSheetHeight = getIntParam("height");
	}
	
	public int getColCount() {
		return colCount;
	}
	
	public String getColName(int col) {
		return colNames.get(col);
	}
	
	public int getColWidth(int col){
		return Integer.parseInt(colWidthControls[col].getValue());
	}
	
	public void setColWidth(int col,int width) {
		colWidthControls[col].setValue(""+width);
	}
	
	public JsArrayString getColumnIds() {
		return colIds;
	}
	
	public int getScrollX() {
		return getIntControlValue(scrollXControl, 0);
	}
	
	public int getScrollY() {
		return getIntControlValue(scrollYControl, 0);
	}
	
	public void setScrollX(int scrollX) {
		scrollXControl.setValue(""+scrollX);
	}
	
	public void setScrollY(int scrollY){
		scrollYControl.setValue(""+scrollY);
	}
	
	public int getSelectedRow() {
		return getIntControlValue(selectionRowControl, -1);
	}
	
	public int getSelectedCol() {
		return getIntControlValue(selectionColControl, -1);
	}
	
	public void setSelectedRow(int selectedRow) {
		selectionRowControl.setValue(""+selectedRow);
	}
	
	public void setSelectedCol(int selectedCol) {
		selectionColControl.setValue(""+selectedCol);
	}
	
	public int getWidth() {
		return spreadSheetWidth;
	}
	
	public int getHeight() {
		return spreadSheetHeight;
	}
	
	public ControlWrapper createControlWrapper(String controlId){
		Element element = DOM.getElementById(controlId);
		if (element == null) {
			throw new RuntimeException("Element not found: id is "+controlId);
		}
		return new ControlWrapper(element);
	}
	
	private int getIntControlValue(ControlWrapper wrapper, int defaultValue) {
		String val = wrapper.getValue();
		if (val == null || "".equals(val)) {
			return defaultValue;
		}
		try {
			return Integer.parseInt(val);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}
		
	private native void initDataObject(String name) /*-{
		this.@com.gwtspreadsheetinput.gwt.client.DynGridData::data = $wnd[name];
	}-*/;
	
	private native int getIntParam(String paramName) /*-{
		return this.@com.gwtspreadsheetinput.gwt.client.DynGridData::data[paramName];
	}-*/;
	
	private native String getStringParam(String paramName) /*-{
		return this.@com.gwtspreadsheetinput.gwt.client.DynGridData::data[paramName];
	}-*/;
	
	private native JsArrayString getStringArrayParam(String paramName) /*-{
		return this.@com.gwtspreadsheetinput.gwt.client.DynGridData::data[paramName];
	}-*/;
	
	
	
}
